/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.Exceptions;

using DotNetNuke.Modules.Articles.Common;
using DotNetNuke.Modules.Articles.Data;
using DotNetNuke.ModuleToolkit.Common;
using DotNetNuke.ModuleToolkit.Framework;

#endregion

namespace DotNetNuke.Modules.Articles.Framework
{

    /// -----------------------------------------------------------------------------
    /// Namespace:  DotNetNuke.Modules.Articles.Framework
    /// Project:    Articles Private Assembly
    /// Class:      ArticleController
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The ArticleController Class represents the Article Business Layer
    /// Methods in this class call methods in the Data Layer
    /// </summary>
    /// <history>
    /// 	[cnurse]	01/31/2006	Created
    /// </history>
    /// -----------------------------------------------------------------------------
    public class ArticleController
    {

        #region Public Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Adds an Article to the DataStore
        /// </summary>
        /// <param name="article">The article to persist</param>
        /// <param name="comment">A comment</param>
        /// <param name="userId">The id of the user</param>
        /// <returns>An integer that represents the Id of the new article</returns>
        /// <history>
        /// 	[cnurse]	01/31/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static int AddArticle(ArticleInfo article, string comment, int userId)
        {
            return DataService.CreateArticle(article.ModuleId, article.SectionId, 
                article.Author, article.Title, article.SubTitle, article.Abstract, article.AbstractImage,
                article.Body, article.Status, article.ViewOrder, comment, userId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Adds a new Version of an Article to the DataStore
        /// </summary>
        /// <param name="article">The article to persist</param>
        /// <param name="comment">A comment</param>
        /// <param name="userId">The id of the user</param>
        /// <history>
        /// 	[cnurse]	01/31/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void AddNewVersion(ArticleInfo article, string comment, int userId)
        {
            DataService.CreateNewVersion(article.ArticleId, article.VersionNo, article.ModuleId, 
                article.SectionId, article.Author, article.Title, article.SubTitle, article.Abstract,
                article.AbstractImage, article.Body, article.ViewOrder, comment, userId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Remove an article from the Data Store
        /// </summary>
        /// <param name="article">The article to remove</param>
        /// <param name="userId">The id of the user</param>
        /// <history>
        /// 	[cnurse]	01/31/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void DeleteArticle(ArticleInfo article, int userId)
        {
            if (article != null)
                DataService.DeleteArticle(article.ArticleId, userId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets all the articles for a single module from the Data Store
        /// </summary>
        /// <param name="moduleId">The id of the module</param>
        /// <returns>A List of ArticleInfo objects</returns>
        /// <history>
        /// 	[cnurse]	01/31/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static List<ArticleInfo> GetAllArticles(int moduleId)
        {
            return CBO.FillCollection<ArticleInfo>(DataService.GetArticles(moduleId));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets all the articles for a single module from the Data Store
        /// </summary>
        /// <param name="moduleId">The id of the module</param>
        /// <param name="pageIndex">The index of the page to return</param>
        /// <param name="pageSize">The size of the page to return</param>
        /// <param name="totalRecords">The total number of records</param>
        /// <returns>A List of ArticleInfo objects</returns>
        /// <history>
        /// 	[cnurse]	08/03/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static List<ArticleInfo> GetAllArticlesByPage(int moduleId, int pageIndex, int pageSize, ref int totalRecords)
        {
            if (pageIndex == Null.NullInteger)
            {
                pageIndex = 0;
                pageSize = Int32.MaxValue;
            }

            return CBO.FillCollection<ArticleInfo>(DataService.GetArticlesByPage(moduleId, pageIndex, pageSize), ref totalRecords);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a single article from the Data Store
        /// </summary>
        /// <param name="articleId">The id of the article</param>
        /// <returns>An ArticleInfo object</returns>
        /// <history>
        /// 	[cnurse]	01/31/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static ArticleInfo GetArticle(int articleId)
        {
            return CBO.FillObject<ArticleInfo>(DataService.GetArticle(articleId));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a List of the Articles created by a user from the Data Store
        /// </summary>
        /// <param name="moduleId">The id of the moduleQualifier</param>
        /// <param name="userId">The id of the User</param>
        /// <param name="status">The status of the articles.</param>
        /// <history>
        /// 	[cnurse]   10/29/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static List<ArticleInfo> GetArticlesByCreatorAndStatus(int moduleId, int userId, PublishedStatus status)
        {
            return CBO.FillCollection<ArticleInfo>(DataService.GetArticlesByCreatorAndStatus(moduleId, userId, status));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets a List of the Articles created by a user from the Data Store
        /// </summary>
        /// <param name="moduleId">The id of the moduleQualifier</param>
        /// <param name="userId">The id of the User</param>
        /// <param name="status">The status of the articles.</param>
        /// <history>
        /// 	[cnurse]   10/29/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static List<ArticleInfo> GetArticlesByCreatorAndStatusByPage(int moduleId, int userId, PublishedStatus status, int pageIndex, int pageSize, ref int totalRecords)
        {
            if (pageIndex == Null.NullInteger)
            {
                pageIndex = 0;
                pageSize = Int32.MaxValue;
            }

            return CBO.FillCollection<ArticleInfo>(DataService.GetArticlesByCreatorAndStatusByPage(moduleId, userId, status, pageIndex, pageSize), ref totalRecords);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets all the articles for a single section from the Data Store
        /// </summary>
        /// <param name="sectionId">The id of the section</param>
        /// <returns>A List of ArticleInfo objects</returns>
        /// <history>
        /// 	[cnurse]	01/31/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static List<ArticleInfo> GetArticlesBySection(int moduleId, int sectionId)
        {
            return CBO.FillCollection<ArticleInfo>(DataService.GetArticlesBySection(moduleId, sectionId));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets all the articles for a single section from the Data Store
        /// </summary>
        /// <param name="sectionId">The id of the section</param>
        /// <param name="pageIndex">The index of the page to return</param>
        /// <param name="pageSize">The size of the page to return</param>
        /// <param name="totalRecords">The total number of records</param>
        /// <returns>A List of ArticleInfo objects</returns>
        /// <history>
        /// 	[cnurse]	08/03/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static List<ArticleInfo> GetArticlesBySectionByPage(int moduleId, int sectionId, int pageIndex, int pageSize, ref int totalRecords)
        {
            if (pageIndex == Null.NullInteger)
            {
                pageIndex = 0;
                pageSize = Int32.MaxValue;
            }

            return CBO.FillCollection<ArticleInfo>(DataService.GetArticlesBySectionByPage(moduleId, sectionId, pageIndex, pageSize), ref totalRecords);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the audit history for an article
        /// </summary>
        /// <param name="articleId">The id of the article</param>
        /// <returns>A List of AuditInfo objects</returns>
        /// <history>
        /// 	[cnurse]	09/12/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static List<AuditInfo> GetAuditHistory(int articleId)
        {
            return CBO.FillCollection<AuditInfo>(DataService.GetArticleAuditHistory(articleId));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the current articles for a module from the Data Store
        /// </summary>
        /// <param name="moduleId">The id of the module</param>
        /// <returns>A List of ArticleInfo objects</returns>
        /// <history>
        /// 	[cnurse]	01/31/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static List<ArticleInfo> GetCurrentArticles(int moduleId)
        {
            return CBO.FillCollection<ArticleInfo>(DataService.GetCurrentArticles(moduleId));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the published version of an article from the Data Store
        /// </summary>
        /// <param name="articleId">The id of the article</param>
        /// <returns>An ArticleInfo object</returns>
        /// <history>
        /// 	[cnurse]	09/09/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static ArticleInfo GetPublishedArticle(int articleId)
        {
            return CBO.FillObject<ArticleInfo>(DataService.GetPublishedArticle(articleId));
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Publishes a submitted article
        /// </summary>
        /// <param name="article">The article being published</param>
        /// <param name="comment">A comment</param>
        /// <param name="userId">The Id of the user</param>
        /// <history>
        /// 	[cnurse]	09/11/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void PublishArticle(ArticleInfo article, string comment, int userId)
        {
            DataService.PublishArticle(article.ArticleId, article.VersionNo, comment, userId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Rejects a submitted article
        /// </summary>
        /// <param name="article">The article being rejected</param>
        /// <param name="comment">A comment</param>
        /// <param name="userId">The Id of the user</param>
        /// <history>
        /// 	[cnurse]	09/11/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void RejectArticle(ArticleInfo article, string comment, int userId)
        {
            DataService.RejectArticle(article.ArticleId, article.VersionNo, comment, userId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Restores a deleted article
        /// </summary>
        /// <param name="article">The article being restored</param>
        /// <param name="comment">A comment</param>
        /// <param name="userId">The Id of the user</param>
        /// <history>
        /// 	[cnurse]	11/01/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void RestoreArticle(ArticleInfo article, string comment, int userId)
        {
            DataService.RestoreArticle(article.ArticleId, article.VersionNo, article.Status, comment, userId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Submits an article for approval
        /// </summary>
        /// <param name="article">The article to submit</param>
        /// <param name="comment">A comment</param>
        /// <param name="userId">The Id of the user</param>
        /// <history>
        /// 	[cnurse]	09/11/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void SubmitArticle(ArticleInfo article, string comment, int userId)
        {
            DataService.SubmitArticle(article.ArticleId, article.VersionNo, comment, userId);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Update an existing article
        /// </summary>
        /// <param name="article">The article to modify</param>
        /// <param name="comment">A comment</param>
        /// <param name="userId">The Id of the user</param>
        /// <history>
        /// 	[cnurse]	01/31/2006	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void UpdateArticle(ArticleInfo article, string comment, int userId)
        {
            DataService.UpdateArticle(article.ArticleId, article.VersionNo, article.SectionId, 
                article.Author, article.Title, article.SubTitle, article.Abstract, 
                article.AbstractImage, article.Body, article.Status, article.ViewOrder, comment, userId);
        }

#endregion

    }
}
